package com.my.test;

import com.my.bean.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author: marshal
 * @date: 2023-05-15 15:21
 */
public class HelloApp {

    /**
     * 通过 xml 配置文件获取 bean
     */
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("hello.xml");
        Hello hello = context.getBean("hello", Hello.class);
        hello.printMessage();
    }

    /**
     * 通过反射获取 bean
     */
    @Test
    public void testReflect() throws Exception {
        // 1. 获取类类型
        Class<?> clazz = Class.forName("com.my.bean.Hello");
        // 2. 实例化对象
        Hello hello = (Hello) clazz.getDeclaredConstructor().newInstance();
        // 3. 设置属性
        hello.setMessage("world");
        hello.printMessage();
    }
}
